<template>
	<view class="goodsDetail">
		<swiper class="swiper" indicator-dots="true" autoplay>
			<swiper-item v-for="(item,index) in swiperData" :key="index">
				<image :src="item.src"></image>
				<!-- <image src="https://gitee.com/mrdai123/pic-go/raw/master/heima_swiper/1.jpg"></image> -->
			</swiper-item>
		</swiper>
		<view class="info">
			<view class="price">
				<text>￥{{info.sell_price}}</text>
				<text>￥{{info.market_price}}</text>
			</view>
			<view class="title">
				<text>{{info.title}}</text>
			</view>
		</view>
		<view class="desc">
			<view>货号：{{info.goods_no}}</view>
			<view>库存：{{info.stock_quantity}}</view>
		</view>
		<view class="detailDesc">
			<view>详细介绍</view>
			<rich-text class="content" :nodes="detailDesc"></rich-text>
		</view>
		<uni-goods-nav class="goodsNav" :fill="true" :options="options" :buttonGroup="buttonGroup"></uni-goods-nav>
	</view>
</template>

<script>
	// 引入解决rich-text富文本图片大小问题
	import formatRichText from '../../utils/rich-text.js'
	export default {
		name: 'goodsDetail',
		data() {
			return {
				swiperData: [],
				info: {},
				detailDesc: '',
				// 底部购买nav
				options: [{
					icon: 'headphones',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺',
					infoBackgroundColor: '#007aff',
					infoColor: "red"
				}, {
					icon: 'cart',
					text: '购物车',
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			}
		},
		onLoad({
			id
		}) {
			// 获取轮播图数据
			this.getImgSwiper(id)
			// 获取详情参数
			this.getInfo(id)
			// 获取详细介绍
			this.getDetailDesc(id)
		},
		methods: {
			async getImgSwiper(id) {
				const res = await this.$myRequest({
					url: `/api/getthumimages/${id}`,
					method: 'get'
				})
				this.swiperData = res.data.message
			},
			async getInfo(id) {
				const res = await this.$myRequest({
					url: `/api/goods/getinfo/${id}`,
					method: 'get'
				})
				this.info = res.data.message[0]
			},
			async getDetailDesc(id) {
				const res = await this.$myRequest({
					url: `/api/goods/getdesc/${id}`,
					method: 'get'
				})
				console.log(res)
				// 解决图片大小问题
				this.detailDesc = formatRichText(res.data.message[0].content)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.goodsDetail {
		width: 750rpx;
		position: relative;
		.swiper {
			width: 720rpx;
			height: 700rpx;
			margin: 10rpx auto;

			// border: 1px solid black;
			image {
				width: 100%;
				height: 100%;
			}
		}

		.info {
			padding: 20rpx;

			.price {
				margin: 20rpx 0;

				text:nth-child(1) {
					color: #d30003;
					font-size: 34rpx;
				}

				text:nth-child(2) {
					color: #aaa;
					text-decoration: line-through;
					font-size: 26rpx;
					margin-left: 15rpx;
				}
			}

			.title {
				font-size: 30rpx;
			}
		}

		.desc {
			border-top: 4rpx solid rgba(0, 0, 0, .1);
			border-bottom: 4rpx solid rgba(0, 0, 0, .1);
			padding: 10rpx 20rpx;
			font-size: 26rpx;
			line-height: 50rpx;
		}

		.detailDesc {
			padding: 20rpx;

			view {
				line-height: 50rpx;
				font-size: 30rpx;
			}
			.content {
				font-size: 26rpx;
			}
		}
		.goodsNav{
			position: fixed;
			width: 750rpx;
			bottom: -6rpx;
		}
	}
</style>
